package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.dto.OrderDto;
import com.itheima.entity.AddressBook;
import com.itheima.entity.Orders;
import com.itheima.entity.User;
import com.itheima.service.OrderService;
import com.itheima.service.UserService;
import com.sun.org.apache.xpath.internal.operations.Or;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @email 1572178030@qq.com
 * @author:黄海宾
 * @date: 2023/2/11  17:08
 */

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private UserService userService;

    /**
     * 用户下单
     *
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders) {
        log.info("订单数据：{}", orders);
        orderService.submit(orders);
        return R.success("下单成功");
    }

    /*
     * 分页查询订单
     * */
    @GetMapping("/userPage")
    public R<Page> queryOrder(int page, int pageSize) {
        Page pageBean = new Page(page, pageSize);
        orderService.page(pageBean);
        return R.success(pageBean);
    }

    @GetMapping("/page")
    public R<Page> queryOrder1(int page, int pageSize, Long number, LocalDateTime beginTime, LocalDateTime endTime) {
        //直接查询数据
        //log.info("orders{}", orders);
        Page pageBean = new Page(page, pageSize);
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ge(beginTime != null, Orders::getOrderTime, beginTime)
                .le(endTime != null, Orders::getCheckoutTime, endTime)
                .like(number != null, Orders::getNumber, number);

        orderService.page(pageBean, queryWrapper);

        //包装到新的Page中
        Page pageBean1 = new Page();
        BeanUtils.copyProperties(pageBean, pageBean1, "records");
        List<Orders> records = pageBean.getRecords();

        List<OrderDto> listOrder = records.stream().map(item -> {
            OrderDto orderDto = new OrderDto();
            BeanUtils.copyProperties(item, orderDto);
            //循环的到对象查找对应的userId对应的名字
            Long userId = item.getUserId();
            User userServiceById = userService.getById(userId);
            orderDto.setOrderName(userServiceById.getName());
            return orderDto;
        }).collect(Collectors.toList());

        pageBean1.setRecords(listOrder);

        return R.success(pageBean1);


    }

    @PutMapping
    public R<String> orderEndit(@RequestBody Orders orders) {
        //根据id查到对象，把对象的状态改为对应的status数据
        log.info("orders{}",orders);
        Long id = orders.getId();
        Orders orderBean = orderService.getById(id);
        orderBean.setStatus(orders.getStatus());
        //对象设置完后，把它跟新数据库
        orderService.updateById(orderBean);

        return R.success("成功");
    }
}
